package com.spider.solitaire.ace.sdk;

import android.util.Log;

import com.spider.solitaire.ace.sdk.ad.SpiderSAVideoRewardCallback;
import com.spider.solitaire.ace.sdk.ad.SpiderSAVideoIAP;
import com.spider.solitaire.ace.sdk.bridge.SpiderSACocosBridge;
import com.spider.solitaire.ace.sdk.ad.SpiderSAVideoManger;
import com.spider.solitaire.ace.sdk.ad.SpiderSAMaxReward;

import org.json.JSONObject;

public class SpiderSAAdSdk {

    public static void showBiddingAd(String ad_source) {

        SpiderSAVideoIAP service = getAdService();

        if (service == null) {
            SpiderSACocosBridge.getInstance().XSSdkCallback("ad_error_spider", "{}");
            return;
        }

        try {
            service.showVideo(SpiderSACocosBridge.mainGameActivity.get(), new SpiderSAVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("ad_source",ad_source);
                        SpiderSACocosBridge.getInstance().XSSdkCallback("ad_over_spider", jsonObject.toString());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onAdError() {
                    SpiderSACocosBridge.getInstance().XSSdkCallback("ad_error_spider", "{}");
                }
            });
        } catch (Exception e) {
            try {
                SpiderSACocosBridge.getInstance().XSSdkCallback("ad_error_spider", "{}");
            } catch (Exception e1) {
            }
        }
    }

    private static SpiderSAVideoIAP getAdService() {
        SpiderSAVideoIAP service = null;
        boolean isMaxReady =  SpiderSAMaxReward.getInstance().isReady();
        if (isMaxReady){
            Log.e("SpiderSa","show max reward");
            service= SpiderSAVideoManger.getReward1Instance();
        }
        return service;
    }

}
